import pyfiglet as figlet # 花体库
import json, time, os, threading, random
import Map, Menu, NewGame, Reloading, Restock, Sold, Story, Calendar, Exit, Warehouse, Asset, Time, Customer, Orders, Archive

os.system ('cls')
print ("欢迎游玩Fish Shop游戏！")

# 游戏变量内容初始化
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
warehouse_level = 1
player_level = 1
player_xp = 0
customer_name = ["蒋心"]
customer_need = {"蒋心1": "土豆", "蒋心2": "鸡肉", "蒋心3": "5号螺帽"}
map = 'A'
money = 50
year = 2000
day = 1
week = 1
month = 1
seventh_shops_discount = 1
discount_num = []
player_warehouse_list = [] # 格式如下：["名称", 单位数量, "单位"]
player_warehouse_list_name = []
original_price = []
all_sold_count = 0
all_day_count = 0
time_hour = 0
time_minute = 0
time_second = 0
acc_num = [0]
times_serve = 0
times_orders = 0
sold_time = 0
story_num = 0
f1 = random.uniform (1.13, 1.16)
f2 = random.uniform (1.14, 1.17)
f3 = random.uniform (1.17, 1.2)
price_up_amplitude = [f1, f2, f3]
orders = [] # 格式如下：["用户", "商品", 单位数量, "单位", 总价, 时效]
save = 0
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
# 重要初始化内容
time_file = os.path.join (os.getcwd (), 'SystemFile', 'saveTime.txt')
if os.path.exists (time_file):
    os.remove (time_file)
level_up_need_xp = [10, 20, 40, 80, 160,
                    260, 350, 500, 650, 800,
                    950, 980, 1000, 1000, 1000,
                    1000, 1000, 1000, 1000, 0]
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

#这里是游戏主界面选择
while (True):
    try :
        Menu.Menu () # 调用主界面选项CLI
        choose = int (input ("选择功能："))
        if (choose in [0,1]):
            os.system ('cls')
            break
        elif (choose == 2):
            os.system ('cls')
            read, player_shop_name, warehouse_level, player_level, player_xp, customer_name, customer_need, map, money, year, day, week, month, seventh_shops_discount, discount_num, player_warehouse_list, player_warehouse_list_name, original_price, all_sold_count, all_day_count, time_hour, time_minute, time_second, acc_num, times_serve, times_orders, sold_time, story_num, price_up_amplitude, orders = Archive.OpenRead ()
            if (read):
                os.system ('cls')
                break
        else :
            raise ValueError
    except ValueError:
        os.system ('cls')
        print ("\n\033[1;31m没有这个功能哦~\033[0m\n") # ANSI转译码

if (choose == 0): # 程序终止
    os.system ('cls')
    os._exit (0)
elif (choose == 1): # 玩家店名输入
    while (True):
        player_shop_name = input ("请输入您的店名(仅能输入英文)：")
        if (player_shop_name and player_shop_name.isalpha()): # x.isalpha判断x是否为全英文字母，是输出True，否则输出False
            break
        else :
            os.system ('cls')
            print ("\n\033[1;31m非法命名，请重新输入。\033[0m\n")
    lg = player_shop_name + "'s"
    player_name = figlet.figlet_format (text = lg , font = 'big_money-ne')
    print("\033[1;92m", player_name)
    print (figlet.figlet_format (text = 'Shop' , font = 'big_money-ne'),"\033[0m\n")
    time.sleep (3)
    print ("\033[38;5;33m正在重整小店......\033[0m")
    os.system ('cls')
    acc_num = NewGame.NewGame () # 新游戏逻辑
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
while (True):
    while (True):
        Customer.AllCustomer (customer_name, customer_need, player_level) # 玩家等级判定，添加新顾客
        time_hour, time_minute, time_second = Time.ComputeTime (time_hour, time_minute, time_second)
        try :
            Menu.MenuPlay () # 调用游戏界面选项CLI
            choose = int (input ("选择功能："))
            if (choose not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
                raise ValueError
            else :
                print ()
                os.system ('cls')
                break
        except ValueError:
            os.system ('cls')
            print ("\n\033[1;31m没有这个功能哦~\033[0m\n")
            
    if (choose == 1 and len (player_warehouse_list_name) > 0): # 营业
        player_level, times_serve, sold_time, all_day_count, all_sold_count, player_warehouse_list, player_warehouse_list_name, warehouse_level, original_price, player_xp, back, money, acc_num = Sold.Sold (player_level, customer_name, customer_need, 
                                                                                                                                                                                        all_sold_count, times_serve, sold_time, all_day_count, player_warehouse_list, 
                                                                                                                                                                                        player_warehouse_list_name, warehouse_level, original_price, 
                                                                                                                                                                                        price_up_amplitude, player_xp, money, acc_num)
        if (back):
            day, week, month, year, seventh_shops_discount = Calendar.Count (day, week, month, year)
            discount_num = Calendar.DiscountNum (day, week, month, seventh_shops_discount)
            f1 = random.uniform (1.13, 1.16)
            f2 = random.uniform (1.14, 1.17)
            f3 = random.uniform (1.17, 1.2)
            price_up_amplitude = [f1, f2, f3] # 价格波动数
            orders = Orders.OrderSpawn (orders, player_level, customer_name, customer_need)
            Archive.AutoSave (player_shop_name, warehouse_level, player_level, player_xp, customer_name, customer_need, map, money, year, day, week, month, seventh_shops_discount, discount_num, player_warehouse_list, player_warehouse_list_name, original_price, all_sold_count, all_day_count, time_hour, time_minute, time_second, acc_num, times_serve, times_orders, sold_time, story_num, price_up_amplitude, orders)
            save = 0
    
    elif (choose == 1 and len (player_warehouse_list_name) == 0):
        print ("仓库是空的，进些货再营业吧！")
    
    elif (choose == 2): # 进货
        save, player_warehouse_list, player_warehouse_list_name, money, original_price = Restock.ChosShop (save, map, money, discount_num, player_warehouse_list, player_warehouse_list_name, warehouse_level, original_price)
    
    elif (choose == 3): # 仓库
        save, warehouse_level, money, acc_num = Warehouse.Warehouse (save, player_warehouse_list, player_warehouse_list_name, warehouse_level, money, acc_num)
    
    elif (choose == 4): # 订单
        save, orders, player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, acc_num, times_orders = Orders.OrderMenu (save, orders, player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, warehouse_level, acc_num, times_orders)
    
    elif (choose == 5): # 日历
        Calendar.Menu (day, week, month, year, discount_num)
    
    elif (choose == 6): # 地图
        save, map, money = Map.MapFunc (save, map, money)
        
    elif (choose == 7): # 资产
        time_hour, time_minute, time_second = Asset.Asset (money, warehouse_level, player_level, customer_name, 
                                                           level_up_need_xp, player_xp, all_sold_count, all_day_count, 
                                                           time_hour, time_minute, acc_num, time_second, player_shop_name,
                                                           times_serve, times_orders, sold_time, story_num)
    
    elif (choose == 8):
        mode = 1
        player_shop_name, save, warehouse_level, player_level, player_xp, customer_name, customer_need, map, money, year, day, week, month, seventh_shops_discount, discount_num, player_warehouse_list, player_warehouse_list_name, original_price, all_sold_count, all_day_count, time_hour, time_minute, time_second, acc_num, times_serve, times_orders, sold_time, story_num, price_up_amplitude, orders = Archive.SaveArchive (player_shop_name, save, mode, warehouse_level, player_level, player_xp, customer_name, 
                                                                                                                                                                                                                                                                                                                                                                                                                   customer_need, map, money, year, 
                                                                                                                                                                                                                                                                                                                                                                                                                   day, week, month, 
                                                                                                                                                                                                                                                                                                                                                                                                                   seventh_shops_discount, discount_num, player_warehouse_list, player_warehouse_list_name, 
                                                                                                                                                                                                                                                                                                                                                                                                                   original_price, all_sold_count, all_day_count, time_hour, 
                                                                                                                                                                                                                                                                                                                                                                                                                   time_minute, time_second, acc_num, times_serve, 
                                                                                                                                                                                                                                                                                                                                                                                                                   times_orders, sold_time, story_num, price_up_amplitude, 
                                                                                                                                                                                                                                                                                                                                                                                                                   orders)
    
    elif (choose == 9): # 剧情
        pass
        
    elif (choose == 0): # 退出
        # save = int (input ("save=")) # 调试
        if (save == 0):
            use = Exit.ExitNo ()
        else :
            use = Exit.ExitYes ()
        
        if (use == 0):
            os.remove (time_file)
            os.system ('cls')
            os._exit (0)